//
//  ChannelViewController.m
//  UI_Project_A
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "ChannelViewController.h"
#import "ChannelVIew.h"
#import "ChannelCell.h"
@interface ChannelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic ,retain)UICollectionView *collecitonChannel;
@property (nonatomic ,retain)UIButton *buttonOfDisMiss;
@property (nonatomic ,retain)UILongPressGestureRecognizer *longPress;


@end

@implementation ChannelViewController


- (void) dealloc{

    [self.collecitonChannel release];
    [self.arrayWithTitle release];
    [self.buttonOfDisMiss release];
    [self.longPress release];
    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCollection];
    [self createLongPress];
}

#pragma mark - 创建CollectionView
- (void)createCollection {
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.sectionInset = UIEdgeInsetsMake(200, 10, 200, 10);
    flowLayout.itemSize = CGSizeMake(60, 40);
    
    
    self.collecitonChannel = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];

    [self.view addSubview:self.collecitonChannel];

    [self.collecitonChannel release];
    
    self.collecitonChannel.dataSource = self;
    self.collecitonChannel.delegate = self;
    self.collecitonChannel.backgroundColor = [UIColor whiteColor];
    
    
    [self.collecitonChannel registerClass:[ChannelCell class] forCellWithReuseIdentifier:@"poolCell"];
    
    ChannelVIew *headView = [[ChannelVIew alloc]init];
    headView.frame =  CGRectMake(0, 0, WIDTH, 64);
    headView.backgroundColor = [UIColor lightGrayColor];
    [self.collecitonChannel addSubview:headView];
    [headView release];
    
    
    
    //ButtonOfMiss
    self.buttonOfDisMiss = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonOfDisMiss.frame = CGRectMake(WIDTH-50, 23, 40, 40);
    
    [self.buttonOfDisMiss setImage:[UIImage imageNamed:@"cha"] forState:UIControlStateNormal];
    
    [self.buttonOfDisMiss addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.buttonOfDisMiss];

}



#pragma mark - 点击返回事件
- (void)nextAction:(UIButton *)button {
    
    [self dismissViewControllerAnimated:YES completion:nil];

}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {



    return self.arrayWithTitle.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    ChannelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolCell" forIndexPath:indexPath];

    cell.label.text = self.arrayWithTitle[indexPath.item];
  
    

    return cell;
}


#pragma mark - 添加长按手势

- (void)createLongPress {
    
    self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(hanldeLongPress:)];
    [self.collecitonChannel addGestureRecognizer:self.longPress];
   
}


#pragma mark - 长安手势事件

- (void)hanldeLongPress:(UILongPressGestureRecognizer *)longPress {

    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
        {
  NSIndexPath *index = [self.collecitonChannel indexPathForItemAtPoint:[longPress locationInView:self.collecitonChannel]];
            if (!index) {
                return;
            }
            
            [self.collecitonChannel beginInteractiveMovementForItemAtIndexPath:index];
            
            break;
        }
            case UIGestureRecognizerStateChanged:
        {
            [self.collecitonChannel updateInteractiveMovementTargetPosition:[longPress locationInView:self.collecitonChannel]];
            
            break;
        }
            case UIGestureRecognizerStateEnded:
        {
            [self.collecitonChannel endInteractiveMovement];
            break;
        }
        default:
            [self.collecitonChannel cancelInteractiveMovement];
            break;
    }





}

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {



}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
